package com.lsfb.signln.My.BoundPhone;

import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.lsfb.signln.Login.LoginActivity;
import com.lsfb.signln.R;
import com.lsfb.signln.Utils.LittleUtils;
import com.lsfb.signln.Utils.URLString;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.T;

import java.util.HashMap;

/**
 * 绑定手机号
 * Created by Administrator on 2018/2/1 0001.
 */
@ContentView(R.layout.aty_bound_phone)
public class BoundPhoneActivity extends LsfbActivity {

    /**
     * 手机号码
     */
    @ViewInject(R.id.aty_et_phone)
    private EditText et_phone;

    /**
     * 验证码
     */
    @ViewInject(R.id.aty_et_code)
    private EditText et_code;

    /**
     * 获取验证码
     */
    @ViewInject(R.id.aty_text_get_code)
    private TextView text_get_code;

    /**
     * 计时器
     */
    private TimeCount time;

    @Override
    public void initView() {
        LittleUtils.setsimpletitlebar(this, "绑定手机号码");
        time = new TimeCount(60000, 1000);
    }

    @Override
    public void initEvent() {

    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.aty_text_get_code, R.id.aty_bound_phone})
    public void onClick(View view) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(et_phone.getWindowToken(), 0);
        imm.hideSoftInputFromWindow(et_code.getWindowToken(), 0);

        switch (view.getId()) {
            //获取验证码
            case R.id.aty_text_get_code:

                getBinderPhonecc();
                break;
            //绑定手机
            case R.id.aty_bound_phone:
                binderPhone();
                break;
        }
    }

    @Override
    public void initData() {

    }

    /**
     * 获取手机验证码
     */
    public void getBinderPhonecc() {
        if (et_phone.getText().toString().isEmpty()) {
            T.showShort(this, "请输入手机号码");
            return;
        }
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("phone", et_phone.getText().toString());
        T.showShort(this,"验证码发送中...");
        new BaseInternet().getData_Get(URLString.GETBINDERPHONECC, hashMap, String.class, new GetBinderPhoneccEvent(), true, false);
    }

    @EventAnnotation
    public void GetBinderPhoneccEvent(GetBinderPhoneccEvent event) {
        switch (event.getCode()) {
            case 200:
                time.start();// 开始计时
                T.showShort(this, "验证码发送成功，请注意收取手机短信。");
                break;
            case 1006:
                T.showShort(this, "您的账号已在其它设备登录，请重登录");
                LittleUtils.exit(this);
                startActivity(new Intent(this, LoginActivity.class));
                break;
            default:
                T.showShort(this, event.getMsg());
                break;
        }
    }

    public void binderPhone() {
        if (et_phone.getText().toString().isEmpty()) {
            T.showShort(this, "请输入手机号码");
            return;
        }
        if (et_code.getText().toString().isEmpty()) {
            T.showShort(this, "请输入验证码");
            return;
        }
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("phone", et_phone.getText().toString());
        hashMap.put("checkcode", et_code.getText().toString());
        new BaseInternet().getData(URLString.BINDERPHONE, hashMap, String.class, new BoundPhoneEvent(), true);
    }

    @EventAnnotation
    public void BoundPhoneEvent(BoundPhoneEvent event) {
        switch (event.getCode()) {
            case 200:
                T.showShort(this, "绑定成功");
                finish();
                break;
            case 1006:
                T.showShort(this, "您的账号已在其它设备登录，请重登录");
                LittleUtils.exit(this);
                startActivity(new Intent(this, LoginActivity.class));
                break;
            default:
                T.showShort(this, event.getMsg());
                break;
        }
    }

    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {// 计时完毕
            text_get_code.setText("获取验证码");
            text_get_code.setTextColor(getResources().getColor(R.color.theme_color));
            text_get_code.setClickable(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程
            text_get_code.setClickable(false);//防止重复点击
            text_get_code.setTextColor(getResources().getColor(R.color.hint_font));
            text_get_code.setText(millisUntilFinished / 1000 + "s后重新获取");
        }

    }
}